<template>
  <div>
    <codemirror
      ref="newCm"
      v-model="code"
      :options="cmOptions"
      @ready="onCmReady"
      style="text-align: left; height: 600px"
    >
    </codemirror>
    <button @click="btn">输出</button>

    <pre class="language-java">
      <code class="language-java" >
        {{codes}}
      </code>
    </pre>
  </div>
</template>
 
<script>
import Prism from "prismjs"; // 引入插件
import "prismjs/themes/prism.css"; // 引入样式

// import CodeEditor from "./codeEditor.vue";
// 引入

import { codemirror } from "vue-codemirror";

// require styles
import "codemirror/lib/codemirror.css";
import "codemirror/mode/javascript/javascript";
import "codemirror/theme/base16-dark.css";
export default {
  components: {
    codemirror,
  },
  mounted() {
    Prism.highlightAll();
  },
  data() {
    return {
      codes: `package student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    public static void starStudentSystem() {

        ArrayList<Student> list = new ArrayList<>();

        loop: while (true) {
            System.out.println("-----------欢饮来到学生管理系统----------");
            System.out.println("1:添加学生");
            System.out.println("2:删除学生");
            System.out.println("3:修改学生");
            System.out.println("4:查询学生");
            System.out.println("5:退出");
            System.out.print("请输入您的选择：");

            Scanner sc = new Scanner(System.in);
            String chose = sc.next();
            switch (chose){
                case "1" -> addStudent(list);
                case "2" -> deleteStudent(list);
                case "3" -> updateStudent(list);
                case "4" -> queryStudent(list);
                case "5" -> {
                    System.out.println("退出");
                    //  break loop;
                    System.exit(0);//停止虚拟机运行
                }
                default -> System.out.println("没有此选项");
            }
        }
    }

    public static void addStudent(ArrayList<Student> list){

        Student s = new Student();

        Scanner sc = new Scanner(System.in);

        String id = null;
        while (true) {
            System.out.println("请输入学生id");
            id = sc.next();
            boolean flag = contains(list, id);
            if(flag){
                System.out.println("id已经存在，请重新录入");
            }else {
                s.setId(id);
                break;
            }
        }

        System.out.println("请输入学生的姓名");
        String name = sc.next();
        s.setName(name);

        System.out.println("请输入学生的年龄");
        int age = sc.nextInt();
        s.setAge(age);

        System.out.println("请输入学生的家庭住址");
        String address = sc.next();
        s.setAddress(address);

        //Student s = new Student(id,name,age,address);

        list.add(s);

        System.out.println("学生信息添加成功");

    }

    public static void deleteStudent(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的id");
        String id = sc.next();
        int index = getIndex(list,id);
        if(index >= 0){
            list.remove(index);
            System.out.println("id为：" + id + "的学生删除成功");
        }else {
            System.out.println("id不存在，删除失败");
        }
    }

    public static void updateStudent(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的学生的id");
        String id = sc.next();

        int index = getIndex(list, id);

        if(index == -1){
            System.out.println("要修改的id" + id + "不存在，请重新输入");
            return;
        }
        Student stu = list.get(index);

        System.out.println("请输入要修改的学生的姓名");
        String newName = sc.next();
        stu.setName(newName);

        System.out.println("请输入要修改的学生的年龄");
        int newAge = sc.nextInt();
        stu.setAge(newAge);

        System.out.println("请输入要修改的学生的家庭住址");
        String newAddress = sc.next();
        stu.setAddress(newAddress);

        System.out.println("学生信息修改成功");

    }

    public static void queryStudent(ArrayList<Student> list){
        if (list.size() == 0){
            System.out.println("当前无学生信息，请添加后再查询");
            return;
        }
        System.out.println("id\t\t姓名\t年龄\t家庭住址");
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getId() + "\t\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getAddress());
        }
    }

    public static boolean contains(ArrayList<Student> list,String id){
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            String sid = stu.getId();
            if(sid.equals(stu)){
                return true;
            }
        }
        return false;
    }

    public static int getIndex(ArrayList<Student> list,String id){

        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            String sid = stu.getId();
            if(sid.equals(stu)){
                return i;
            }
        }
        return -1;
    }

}
`,
      // cmOptions: {
      //   tabSize: 4, // tabsize默认为4
      //   styleActiveLine: true,
      //   lineNumbers: true, // 代码行数字
      //   line: true,
      //   mode: "text/javascript", // 选择代码语言，我这里选的sql
      //   extraKeys: { Ctrl: "autocomplete" }, //自动提示配置
      //   lineWrapping: true, // 自动换行
      //   theme: "solarized light", // 主题根据需要自行配置
      // },
      code: "const a = 10",
      cmOptions: {
        // codemirror options
        tabSize: 4,
        mode: "text/javascript",
        theme: "base16-dark",
        lineNumbers: true,
        line: true,
        // more codemirror options, 更多 codemirror 的高级配置...
      },
    };
  },
  methods: {
    onCmReady(cm) {
      // 设置代码编辑框宽和高
      this.$refs.newCm.codemirror.setSize("-webkit-fill-available", "auto");
    },
    btn() {
      console.log(this.code);
    },
  },
  computed: {
    codemirror() {
      return this.$refs.myCm.codemirror;
    },
  },
  // mounted() {
  // }
};
</script>